package org.openbravo.pool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;

import org.openbravo.data.FieldProvider;
import org.openbravo.database.ConnectionProvider;

import utility.ConvertToLower;
import dbmanager.DBManager;

public class RequestParameter 
{
	
	ConnectionProvider conn;
	public RequestParameter(ConnectionProvider conn)
	{
		this.conn = conn;
	}
	
	public String COMMAND;
	public String windowId;  
	public String TabId;
	public String ColumnId;
	String tablename;
	boolean result=true;
	boolean isCommand;
	String poolError;
	Hashtable<Object, Object>requestHash=new Hashtable<Object, Object>();
	public void setRequest(HttpServletRequest request)
	{ 
		  Enumeration< Object >enum1 = request.getParameterNames();
		  while(enum1.hasMoreElements())
		  {
			
			  String param = enum1.nextElement().toString();
			  String value = request.getParameter(param);
			  if (param.equalsIgnoreCase("Command"))
		  {
		    COMMAND = value;	
		     // requestHash.put(COMMAND, value);
		  }
		  else if (param.equalsIgnoreCase("inpwindowId"))
		  {
		     windowId = value;
			//	requestHash.put(windowId, value);
		  }
		  else if (param.equalsIgnoreCase("inpTableId"))
		  {
				//requestHash.put(TabId, value);
			  TabId = value;
		  }
		  else
		  {
			  param = param.replace("inp", "");
			  	  param = transformFieldToColumn(param);
				  requestHash.put(param, value);
			  }
		   }
		  System.out.println(requestHash);
		  getCommand();
		if(isCommand)
		{
			tablename = getTableName(TabId);
			callPool();
		  	 
		}
	  	   
	}
	private void getCommand() 
	{
		try 
		{
			if(COMMAND.equalsIgnoreCase("SAVE_NEW_RELATION")||COMMAND.equalsIgnoreCase("SAVE_NEW_NEW")||COMMAND.equalsIgnoreCase("SAVE_NEW_EDIT"))
			{
				 isCommand = true;
				 COMMAND = "add";
			}
		} catch (Exception e) {
			isCommand = false;
		}
		
	}
	private String transformFieldToColumn(String param) 
	{
		return param;
	}
	public Hashtable<Object, Object> getRequest()
	{
		return requestHash;
	}
	void callPool()
	{
		Pool pool = new Pool();
		pool.setData(tablename, COMMAND);
		requestHash = ConvertToLower.convertHashKey(requestHash);
		pool.setPropertyMap(requestHash);
		pool.Process();
		Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
		hash.putAll(pool.getResult());
		Enumeration< Object > enum1 = hash.keys();
		try
		{
			poolError = hash.get("ErrorInput").toString();
			result = false;
		}
		catch (Exception e) {
			result = true;
		}
	/*	while(enum1.hasMoreElements())
		{
			String key = enum1.nextElement().toString();
			poolError = poolError + hash.get(key).toString() + "\n";
		}
		*/
	}
	public boolean getResult() 
	{
		return result;
	}
	String getTableName(String id)
	{
		try 
		{
			Statement st = conn.getStatement();
			ResultSet rs = st.executeQuery("select tabl.tablename from ad_table tabl where tabl.ad_table_id = '"+id+"'");
			rs.next();
			return rs.getString("tablename");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	public String getError() {
		return poolError;
	}
}

/***
 * try 
    {
		RequestParameter req = new RequestParameter(this);
		req.setRequest(request);
		if(!req.getResult())
		{
			bdErrorGeneral(response, "Error From POOL", req.getError());
			return;
		}
	} catch (Exception e) {
	}
	
**/
